package cn.wolfcode.wolf2w.service.impl;


import cn.wolfcode.wolf2w.domain.StrategyComment;
import cn.wolfcode.wolf2w.query.StrategyCommentQuery;
import cn.wolfcode.wolf2w.repository.StrategyCommentRepository;
import cn.wolfcode.wolf2w.service.IStrategyCommentService;
import cn.wolfcode.wolf2w.util.DBHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

@Service
public class StrategyCommentImpl implements IStrategyCommentService {


    //在这里引用后期后台项目引用会导致报错
   /* @Value("${sms.url}")
    private String smsUrl;
    @Value("${sms.Key")
    private String appKey;*/


    @Autowired
    private StrategyCommentRepository repository;
    @Autowired
    private MongoTemplate template;

    @Override
    public void save(StrategyComment strategyComment) {
        strategyComment.setId(null);
        repository.save(strategyComment);
    }



    @Override
    public StrategyComment get(String id) {
        StrategyComment strategyComment = repository.findById(id).get();
        return strategyComment;
    }

    @Override
    public List<StrategyComment> list() {
        List<StrategyComment> strategyComments = repository.findAll();
        return strategyComments;
    }

    //攻略评论分页查询
    @Override
    public Page<StrategyComment> query(StrategyCommentQuery qo) {
        Query query=new Query();
        if (StringUtils.hasLength(qo.getStrategyId)){
            query.addCriteria(Criteria.where("strategyId").is(qo.getStrategyId));
        }
        Pageable pageable= PageRequest.of(qo.getCurrentPage()-1,qo.getPageSize(), Sort.Direction.DESC,"createTime");
        query.with(pageable);
        List<StrategyComment> list = template.find(query, StrategyComment.class);
        return DBHelper.query(template,StrategyComment.class,query,pageable);
    }

    @Override
    public void commentThumb(String cid, String uid) {
        //查询评论对象，获取点赞用户集合
        StrategyComment comment = this.get(cid);
        //判断当前用户id是否存在集合中
        List<String> thumbuplist = comment.getThumbuplist();
        if (!thumbuplist.contains(uid)){
            //如果不存在，表示点赞操作  点赞数+1   用户id加入集合中
            comment.setThumbupnum(comment.getThumbupnum()+1);
            thumbuplist.add(uid);
        }else {
            //如果存在表示取消点赞  点赞数-1  用户id移除集合
            comment.setThumbupnum(comment.getThumbupnum()-1);
            thumbuplist.remove(uid);
        }
        //更新
        this.updateComment(comment);
    }

    @Override
    public void updateComment(StrategyComment comment) {
        if (!StringUtils.hasLength(comment.getId())){
            repository.save(comment);
        }else {
            StrategyComment comment1 = this.get(comment.getId());
            comment1.setThumbupnum(comment.getThumbupnum());
            comment1.setThumbuplist(comment.getThumbuplist());
            repository.save(comment1);
        }

    }
}
